package com.cwd.arm.common.util;

import java.math.BigDecimal;

/**
 * Created by yan on 21-12-4.
 */
public class BigDecimalUtil {

    public static final BigDecimal HUNDRED = new BigDecimal("100");

    public static final BigDecimal W = new BigDecimal("10000");

    /**
     * 求和
     *
     * @param oldAmount
     * @param addAmount
     * @return
     */
    public static BigDecimal add(BigDecimal oldAmount, BigDecimal addAmount) {
        if (oldAmount != null && addAmount != null) {
            return oldAmount.add(addAmount);
        }
        if (oldAmount == null && addAmount != null) {
            return addAmount;
        }
        if (oldAmount != null && addAmount == null) {
            return oldAmount;
        }
        return oldAmount;
    }

    /**
     * 求差
     *
     * @param amount1
     * @param amount2
     * @return
     */
    public static BigDecimal sub(BigDecimal amount1, BigDecimal amount2) {
        if (amount1 != null && amount2 != null) {
            return amount1.subtract(amount2);
        }
        if (amount1 == null && amount2 != null) {
            return BigDecimal.ZERO.subtract(amount2);
        }
        if(amount1 !=null && amount2 == null){
            return amount1;
        }
        return BigDecimal.ZERO;
    }

    /**
     * @param oldAmount
     * @param newAmount
     * @return
     */
    public static BigDecimal diff(BigDecimal oldAmount, BigDecimal newAmount) {
        if (oldAmount != null && newAmount != null) {
            return newAmount.subtract(oldAmount);
        }
        if (oldAmount == null && newAmount != null) {
            return newAmount;
        }
        if (oldAmount != null && newAmount == null) {
            return BigDecimal.ZERO.subtract(oldAmount);
        }
        return BigDecimal.ZERO;
    }

    /**
     * 除,四舍五入,余两位小数
     *
     * @param numerator
     * @param denominator
     * @return
     */
    public static BigDecimal div2(BigDecimal numerator, BigDecimal denominator) {
        if(denominator.compareTo(BigDecimal.ZERO) == 0){
            return BigDecimal.ZERO;
        }
        return numerator.divide(denominator, 2, BigDecimal.ROUND_HALF_UP);
    }

    public static boolean isEqual(BigDecimal amount1, BigDecimal amount2) {
        if (amount1 == null && amount2 == null) {
            return true;
        }
        if (amount1 == null && amount2 != null) {
            return false;
        }
        if (amount2 == null && amount1 != null) {
            return false;
        }
        if (amount2.compareTo(amount1) == 0) {
            return true;
        }
        return false;
    }

    /**
     * 这个amount是不是已经产生了金额
     *
     * @param amount
     * @return
     */
    public static boolean hasRecord(BigDecimal amount) {
        if (amount == null) {
            return false;
        }
        if (amount.compareTo(BigDecimal.ZERO) == 0) {
            return false;
        }
        return true;
    }

    /**
     * 转为负数
     *
     * @return
     */
    public static BigDecimal minus(BigDecimal amount) {
        if (amount == null) {
            return BigDecimal.ZERO;
        }
        return BigDecimal.ZERO.subtract(amount);
    }

    public static BigDecimal scaleRate(BigDecimal rate) {
        return rate.setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 格式化金额
     * @param newScale 小数点
     * @param b
     * @return
     */
    public static BigDecimal format(int newScale,BigDecimal b) {
        return b.setScale(newScale, BigDecimal.ROUND_HALF_UP);
    }

    public static BigDecimal format(BigDecimal b) {
        return format(4,b);
    }

    public static BigDecimal getWithTaxAmount(BigDecimal amount,BigDecimal taxPoint){
        BigDecimal tax = BigDecimal.ONE.add(taxPoint);
        return div2(amount,tax);
    }

    public static String convertToRate(BigDecimal amount){
        return format(2,amount).multiply(HUNDRED)+"%";
    }

    /**
     * 元转万
     * @param amount 元
     * @return
     */
    public static BigDecimal convertYToW(BigDecimal amount){
        return amount.divide(W,6,BigDecimal.ROUND_HALF_UP);
    }


}
